package com.ews.onlineexchange.service;

import java.io.Serializable;
import java.util.Calendar;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.Query;

@Named
@ApplicationScoped
public class ProfessionRatingRecordService implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Inject
	EntityManager entityManager;
	
	private static final String GET_RATING_BY_SUPPLIER = " " +
			"  SELECT count(professionRatingRecord)  " +
			" FROM ProfessionRatingRecord professionRatingRecord"+
			" WHERE professionRatingRecord.supplier.id = :supplierId " +
			" AND  professionRatingRecord.year = :year ";
	
	public boolean isRatable(String supplierId) {
		Query query = entityManager.createQuery(GET_RATING_BY_SUPPLIER);
		query.setParameter("supplierId", supplierId);
		query.setParameter("year", Calendar.getInstance().get(Calendar.YEAR));
		Long count = (Long)query.getSingleResult();
		if(count > 0) return false;
		else return true;
	}
	
}
